using System.Threading;
using System.Threading.Tasks;
using Dapr.Client;
using EasyDapr.Infrastructure;
using EasyDapr.Infrastructure.App.Dtos;
using EasyDapr.Infrastructure.Attribute;
using EasyDapr.Infrastructure.Helpers;
using EasyDapr.Infrastructure.Response;
using Microsoft.Extensions.Logging;

namespace InventoryService.Infrastructure.Gateway
{
    [DaprComponent]
    public class DaprIdentityGateway
    {
        private readonly ILogger<DaprIdentityGateway> _logger;
        private readonly DaprClient _daprClient;

        private const string Channel = "InventoryCreated";

        public DaprIdentityGateway(DaprClient daprClient, ILogger<DaprIdentityGateway> logger)
        {
            _daprClient = daprClient;
            _logger = logger;
        }

        /// <summary>
        /// 读取用户信息
        /// </summary>
        /// <param name="id">Id</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<UserDto>> GetUserByIdAsync(string id, CancellationToken cancellationToken)
        {
            return await ResultHelper.CommonResultAsync(async () =>
            {
                var rsp = await _daprClient.InvokeMethodAsync<object, ApiResult<UserDto>>(
                    Consts.IdentityAppId,
                    "get-user-by-id", new
                    {
                        UserId = id
                    },
                    cancellationToken);
                return rsp;
            });
        }
    }
}